package com.gongpingjialoan.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.PhotoItem;
import com.gongpingjialoan.bean.PhotoModel;
import com.gongpingjialoan.widget.MBaseAdapter;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/8/2.
 */
public class PhotoSelectorAdapter extends MBaseAdapter<PhotoModel> {

    private int itemWidth;

    private int horizentalNum = 3;

    private PhotoItem.onPhotoItemCheckedListener listener;

    private AbsListView.LayoutParams itemLayoutParams;

    private PhotoItem.onItemClickListener mCallback;

    private View.OnClickListener cameraListener;

    private boolean isSingle = false;

    private PhotoSelectorAdapter(Context context, ArrayList<PhotoModel> models) {
        super(context, models);
    }

    public PhotoSelectorAdapter(Context context, ArrayList<PhotoModel> models, int screenWidth,
                                PhotoItem.onPhotoItemCheckedListener listener, PhotoItem.onItemClickListener mCallback, View.OnClickListener cameraListener , boolean isSingle) {
        this(context, models);
        setItemWidth(screenWidth);
        this.listener = listener;
        this.mCallback = mCallback;
        this.cameraListener = cameraListener;
        this.isSingle = isSingle;
    }

    public void setItemWidth(int screenWidth) {
        int horizentalSpace = context.getResources().getDimensionPixelSize(R.dimen.sticky_item_horizontalSpacing);
        this.itemWidth = (screenWidth - (horizentalSpace * (horizentalNum - 1))) / horizentalNum;
        this.itemLayoutParams = new AbsListView.LayoutParams(itemWidth, itemWidth);
    }

    public void addData(PhotoModel model) {
        models.add(0, model);
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        PhotoItem item = null;
        if (convertView == null || !(convertView instanceof PhotoItem)) {
            item = new PhotoItem(context, listener);
            item.setLayoutParams(itemLayoutParams);
            convertView = item;
        } else {
            item = (PhotoItem) convertView;
        }
        item.setImageDrawable(models.get(position));
        item.setCheckboxVis(isSingle);
        item.setSelected(models.get(position).isChecked());
        item.setOnClickListener(mCallback, position);
        return convertView;
    }
}
